|
1
|
|
|
const path = require('path'); |
|
2
|
|
|
const fs = require('fs'); |
|
3
|
|
|
const jsonfile = require('jsonfile'); |
|
4
|
|
|
|
|
5
|
|
|
const baseDir = require('./baseDir'); |
|
6
|
|
|
const packages = require('../sample/dependencies'); |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Auto generator for configuration files |
|
10
|
|
|
* @class Generate |
|
11
|
|
|
*/ |
|
12
|
|
|
class Generate { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param {String} extend - extend the pworking dorectory |
|
16
|
|
|
* Creates an instance of Generate. |
|
17
|
|
|
* @memberOf Generate |
|
18
|
|
|
*/ |
|
19
|
|
|
constructor(extend = '') { |
|
20
|
|
|
this.mainpath = path.join(__dirname, '../'); |
|
21
|
|
|
this.filepath = `${baseDir.getCurrentWorkingDir()}/${extend}`; |
|
22
|
|
|
this.basename = baseDir.getCurrentDirectoryBase(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* webpack - Generate a webpack config file from the sample |
|
27
|
|
|
* @returns {Promise} if successful, resolves and pass in the |
|
28
|
|
|
* configuration as argument. Else reject and pass the error |
|
29
|
|
|
* @memberOf Generate |
|
30
|
|
|
*/ |
|
31
|
|
|
webpack() { |
|
32
|
|
|
return new Promise((resolve, reject) => { |
|
33
|
|
|
fs.readFile( |
|
34
|
|
|
path.join(this.mainpath, '/sample/webpack.config.sample'), |
|
35
|
|
|
(err, data) => { |
|
36
|
|
|
fs.writeFile( |
|
37
|
|
|
path.join( |
|
38
|
|
|
this.filepath, |
|
39
|
|
|
'/webpack.config.js'), |
|
40
|
|
|
data.toString(), |
|
41
|
|
|
(err) => { |
|
42
|
|
|
if (err) { |
|
43
|
|
|
return reject(err); |
|
44
|
|
|
} |
|
45
|
|
|
return resolve(data.toString()); |
|
46
|
|
|
}); |
|
47
|
|
|
}); |
|
48
|
|
|
}); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* babelrc - Generate a .babelrc file from the sample |
|
53
|
|
|
* @returns {Promise} if successful, resolves and pass in the |
|
54
|
|
|
* configuration as argument. Else reject and pass the error |
|
55
|
|
|
* @memberOf Generate |
|
56
|
|
|
*/ |
|
57
|
|
|
babelrc() { |
|
58
|
|
|
return new Promise((resolve, reject) => { |
|
59
|
|
|
fs.readFile( |
|
60
|
|
|
path.join(this.mainpath, '/sample/babelrc.sample'), |
|
61
|
|
|
(err, data) => { |
|
62
|
|
|
fs.writeFile( |
|
63
|
|
|
path.join( |
|
64
|
|
|
this.filepath, |
|
65
|
|
|
'/.babelrc'), |
|
66
|
|
|
data.toString(), |
|
67
|
|
|
(err) => { |
|
68
|
|
|
if (err) { |
|
69
|
|
|
return reject(err); |
|
70
|
|
|
} |
|
71
|
|
|
return resolve(data.toString()); |
|
72
|
|
|
}); |
|
73
|
|
|
}); |
|
74
|
|
|
}); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* package - Generate a package.json file |
|
79
|
|
|
* @param {Object} setupInfo - setup information provided by user |
|
80
|
|
|
* @returns {Promise} if successful, resolves and pass in the |
|
81
|
|
|
* configurations as argument. Else reject and pass the error |
|
82
|
|
|
* @memberOf Generate |
|
83
|
|
|
*/ |
|
84
|
|
|
package(setupInfo) { |
|
85
|
|
|
return new Promise((resolve, reject) => { |
|
86
|
|
|
let dependencies; |
|
87
|
|
|
const sortedDependencies = {}; |
|
88
|
|
|
if (setupInfo.express === 'y' || setupInfo.express === 'yes') { |
|
89
|
|
|
dependencies = Object.assign( |
|
90
|
|
|
{}, |
|
91
|
|
|
packages.dependencies, |
|
92
|
|
|
packages.express |
|
93
|
|
|
); |
|
94
|
|
|
} else { |
|
95
|
|
|
dependencies = packages.dependencies; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
Object.keys(dependencies).sort().forEach((dependency) => { |
|
99
|
|
|
sortedDependencies[dependency] = dependencies[dependency]; |
|
100
|
|
|
}); |
|
101
|
|
|
|
|
102
|
|
|
const packageEx = { |
|
103
|
|
|
name: setupInfo.name, |
|
104
|
|
|
version: '1.0.0', |
|
105
|
|
|
description: (!setupInfo.description || setupInfo.description === '') ? |
|
106
|
|
|
'' : setupInfo.description, |
|
107
|
|
|
main: (!setupInfo.main || setupInfo.main === '') ? |
|
108
|
|
|
'index.js' : setupInfo.main, |
|
109
|
|
|
scripts: { |
|
110
|
|
|
test: '', |
|
111
|
|
|
start: 'webpack', |
|
112
|
|
|
'start-dev': 'webpack-dev-server --config webpack.config.js --open --content-base dist/' |
|
113
|
|
|
}, |
|
114
|
|
|
keywords: [], |
|
115
|
|
|
author: (!setupInfo.author || setupInfo.author === '') ? |
|
116
|
|
|
'' : setupInfo.author, |
|
117
|
|
|
license: (!setupInfo.license || setupInfo.license === '') ? |
|
118
|
|
|
'MIT' : setupInfo.license, |
|
119
|
|
|
dependencies: sortedDependencies, |
|
120
|
|
|
devDependencies: packages.devDependencies |
|
121
|
|
|
}; |
|
122
|
|
|
jsonfile.writeFile( |
|
123
|
|
|
path.join( |
|
124
|
|
|
this.filepath, |
|
125
|
|
|
'/package.json'), |
|
126
|
|
|
packageEx, { spaces: 2 }, (err) => { |
|
127
|
|
|
if (err) { |
|
128
|
|
|
return reject(err); |
|
129
|
|
|
} |
|
130
|
|
|
return resolve(packageEx); |
|
131
|
|
|
}); |
|
132
|
|
|
}); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* eslintrc - Generate a .eslintrc file from the sample |
|
137
|
|
|
* @returns {Promise} if successful, resolves and pass in the |
|
138
|
|
|
* configuration as argument. Else reject and pass the error |
|
139
|
|
|
* @memberOf Generate |
|
140
|
|
|
*/ |
|
141
|
|
|
eslintrc() { |
|
142
|
|
|
return new Promise((resolve, reject) => { |
|
143
|
|
|
fs.readFile( |
|
144
|
|
|
path.join(this.mainpath, '/sample/eslintrc.sample'), |
|
145
|
|
|
(err, data) => { |
|
146
|
|
|
fs.writeFile( |
|
147
|
|
|
path.join( |
|
148
|
|
|
this.filepath, |
|
149
|
|
|
'/.eslintrc'), |
|
150
|
|
|
data.toString(), |
|
151
|
|
|
(err) => { |
|
152
|
|
|
if (err) { |
|
153
|
|
|
return reject(err); |
|
154
|
|
|
} |
|
155
|
|
|
resolve(data.toString()); |
|
|
|
|
|
|
156
|
|
|
}); |
|
157
|
|
|
}); |
|
158
|
|
|
}); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* all - Generates package.json, webpack.config.js, .eslintrc, .babelrc |
|
163
|
|
|
* @param {Object} setupInfo - setup information provided by user |
|
164
|
|
|
* @returns {Promise} if successful, resolves the promise. |
|
165
|
|
|
* Else reject and pass the error |
|
166
|
|
|
* @memberOf Generate |
|
167
|
|
|
*/ |
|
168
|
|
|
all(setupInfo) { |
|
169
|
|
|
return new Promise((resolve, reject) => { |
|
170
|
|
|
this.webpack().then(() => { |
|
171
|
|
|
this.eslintrc().then(() => { |
|
172
|
|
|
this.package(setupInfo).then(() => { |
|
173
|
|
|
this.babelrc().then(() => { |
|
174
|
|
|
resolve(); |
|
175
|
|
|
}); |
|
176
|
|
|
}); |
|
177
|
|
|
}); |
|
178
|
|
|
}).catch((err) => { |
|
179
|
|
|
reject(err); |
|
180
|
|
|
}); |
|
181
|
|
|
}); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
module.exports = Generate; |
|
186
|
|
|
|